<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>药品管理系统 - 注册</title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
      font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .register-container {
      width: 450px;
      padding: 40px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
    }
    .register-title {
      text-align: center;
      font-size: 28px;
      color: #409EFF;
      margin-bottom: 30px;
      font-weight: bold;
    }
    .register-form {
      width: 100%;
    }
    .register-button {
      width: 100%;
      height: 45px;
      font-size: 16px;
    }
    .register-logo {
      text-align: center;
      margin-bottom: 25px;
    }
    .logo {
      width: 80px;
      height: 80px;
      background: linear-gradient(45deg, #409EFF, #67C23A);
      border-radius: 50%;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      color: white;
      font-size: 32px;
      font-weight: bold;
    }
    .login-link {
      text-align: center;
      margin-top: 20px;
      font-size: 14px;
      color: #606266;
    }
    .login-link a {
      color: #409EFF;
      text-decoration: none;
      font-weight: 500;
    }
    .login-link a:hover {
      text-decoration: underline;
    }
    .el-input__inner {
      height: 45px;
    }
    .role-select {
      width: 100%;
    }
  </style>
</head>
<body>
<div id="register-app">
  <div class="register-container">
    <div class="register-logo">
      <div class="logo">药</div>
    </div>
    <h2 class="register-title">用户注册</h2>
    <el-form :model="registerForm" :rules="registerRules" ref="registerForm" class="register-form">
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" placeholder="用户名" prefix-icon="el-icon-user" size="medium"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="registerForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" show-password size="medium"></el-input>
      </el-form-item>

      <el-form-item prop="confirmPassword">
        <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码" prefix-icon="el-icon-lock" show-password size="medium"></el-input>
      </el-form-item>

      <el-form-item prop="role">
        <el-select v-model="registerForm.role" placeholder="选择角色" class="role-select" size="medium">
          <el-option label="销售员" value="sales"></el-option>
          <el-option label="库存管理员" value="inventory"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="register-button" @click="handleRegister" :loading="loading">注册</el-button>
      </el-form-item>
    </el-form>

    <div class="login-link">
      已有账号？<a href="login.html">立即登录</a>
    </div>
  </div>
</div>

<script>
  new Vue({
    el: '#register-app',
    data() {
      const validateConfirmPassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        registerForm: {
          username: '',
          password: '',
          confirmPassword: '',
          role: ''
        },
        registerRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, validator: validateConfirmPassword, trigger: 'blur' }
          ],
          role: [
            { required: true, message: '请选择角色', trigger: 'change' }
          ]
        },
        loading: false
      };
    },
    methods: {
      handleRegister() {
        this.$refs.registerForm.validate((valid) => {
          if (valid) {
            this.loading = true;

            const registerData = {
              username: this.registerForm.username,
              password: this.registerForm.password,
              role: this.registerForm.role
            };

            axios.post('/pms/user/add', registerData)
                    .then(response => {
                      this.loading = false;
                      if (response.data.success) {
                        this.$message.success(response.data.message);
                        setTimeout(() => {
                          window.location.href = 'login.html';
                        }, 1500);
                      } else {
                        this.$message.error(response.data.message);
                      }
                    })
                    .catch(error => {
                      this.loading = false;
                      console.error('注册失败:', error);
                      this.$message.error('注册失败，请检查网络连接');
                    });
          }
        });
      }
    }
  });
</script>
</body>
</html>